﻿//发送短信验证弹出层JS
var RegexEnum =
{
    integer: "^-?[1-9]\\d*$", 				//整数
    integer_Z: "^[1-9]\\d*$", 				//正整数
    integer_F: "^-[1-9]\\d*$", 				//负整数
    num: "^([+-]?)\\d*\\.?\\d+$", 		//数字
    num_Z: "^[1-9]\\d*|0$", 				//正数（正整数 + 0）
    num_F: "^-[1-9]\\d*|0$", 				//负数（负整数 + 0）
    decmal: "^([+-]?)\\d*\\.\\d+$", 		//浮点数
    decmal_Z: "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$", //正浮点数
    decmal_F: "^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)$", //负浮点数
    decmal3: "^-?([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0)$", //浮点数
    decmal4: "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0$", //非负浮点数（正浮点数 + 0）
    decmal5: "^(-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*))|0?.0+|0$", //非正浮点数（负浮点数 + 0）

    email: "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$", //邮件
    color: "^[a-fA-F0-9]{6}$", 			//颜色
    url: "^(http[s]?:\\/\\/)?([\\w-]+\\.)+[\\w-]+([\\w-./?%&=]*)?$", //网址 url
    chinese: "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$", 				//仅中文
    ascii: "^[\\x00-\\xFF]+$", 			//仅ACSII字符
    zipcode: "^\\d{6}$", 					//邮编
    mobile: "^(13|15|18)[0-9]{9}$", 			//手机
    tel: "^(((([0\\+]\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?)|((4|8)00-\\d{3,4}-\\d{4,5}))$", //电话号码(包括验证国内区号,国际区号,分机号,400或800电话)
    tel_mobile: "(^(13|15|18)[0-9]{9}$)|(^(((([0\\+]\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?)|((4|8)00-\\d{3,4}-\\d{4,5}))$)", //手机和电话号码
    fax: "^[+]{0,1}(\\d){1,3}[ ]?([-]?((\\d)|[ ]){1,12})+$", //传真
    qq: "^[1-9]*[1-9][0-9]*$", 			//QQ号码
    ip4: "^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$", //ip地址
    notempty: "^\\S+$", 					//非空
    picture: "(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$", //图片
    rar: "(.*)\\.(rar|zip|7zip|tgz)$", 							//压缩文件
    date: "^\\d{4}(\\-|\\/|\.)\\d{1,2}\\1\\d{1,2}$", 				//日期
    username: "^\\w+$", 					//用来用户注册。匹配由数字、26个英文字母或者下划线组成的字符串
    letter: "^[A-Za-z]+$", 				//字母
    letter_u: "^[A-Z]+$", 				//大写字母
    letter_l: "^[a-z]+$", 				//小写字母
    idcard: "^[1-9]([0-9]{14}|[0-9]{17})$", //身份证
    //只能输入字母、数字或符号的组合密码，不能单独使用字母、数字或符号(需取反)
    registerPwd: "^((\\d+)|([a-zA-Z])|([\\u0021-\\u0029]|[\\u002a]|[\\u003a]|[\\u002b]|[\\u003b]|[\\u005b]|[\\u007b]|[\\u002c]|[\\u003c]|[\\u002d]|[\\u003d]|[\\u005d]|[\\u007d]|[\\u002e]|[\\u003e]|[\\u005e]|[\\u007e]|[\\u002f]|[\\u003f]|[\\u005f]|[\\u0060]|[\\u0040]))$"
};

(function () {
    //定时器当前时间
    var _time = 0;

    //是否可以发送短信
    var _flag = true;

    //定时器函数
    var _setTimeOut;

    //全局变量(弹出层的实例)
    var conInstance = null;

    //静态配直节
    var _StaticConfig = {

        //定时器倒计时时间起点
        FixTime: 60,

        //标签的Jquery对象的ID
        TagIDCollection: {

            //发送验证码提示
            err_send: "err_PopSend",

            //发送按钮
            Btn_Send: "Btn_PopSend",

            //验证码框
            Txt_Verify: "VerifyCode",

            //验证码提示框
            err_Verify: "err_VerifyCode"

        }
    };

    //兼容性函数（对象或ID）
    function _Compatibility(obj) {
        var _value = "";
        if (typeof (obj) == "object") {
            _value = $(obj).val();
        }
        else {
            _value = $("#" + obj).val();
        }
        return _value;
    }

    //定时器
    function _Timing() {
        $("#" + _StaticConfig.TagIDCollection.Btn_Send).val(_time + "秒后可重新发送");

        if (_time == 0) {
            _lockSendCancel();
        }
        else {
            _time--;
            _lockSend();
            _setTimeOut = setTimeout(arguments.callee, 1000);
        }
    }

    //检查验证码的格式
    function _checkVerifyCodeFormat(obj) {
        var value = _Compatibility(obj);

        if (value.length == 0) {
            _showNote(_StaticConfig.TagIDCollection.err_verifyCode, "验证码为空！", true);
        }
        else {
            _hideNote(_StaticConfig.TagIDCollection.err_verifyCode);

            return true;
        }

        return false;
    }

    //呈现提示
    //ID：容器ID，msg:内容，box:样式
    function _showMsg(ID, msg, box) {
        var obj = $("#" + ID);
        obj.prop("class", box);
        obj.text(msg);
    }

    //隐藏提示
    function _hideNote(ID) {
        var obj = $("#" + ID);

        obj.text("");
        obj.hide();
    }

    //锁定发送信息
    function _lockSend() {
        //解除绑定事件，设置按钮不可使用
        $("#" + _StaticConfig.TagIDCollection.Btn_Send).unbind();
        $("#" + _StaticConfig.TagIDCollection.Btn_Send).attr("disabled", true);
    }

    //取消锁定发送信息
    function _lockSendCancel() {
        $("#" + _StaticConfig.TagIDCollection.Btn_Send).click(_sendVerifyCode);
        $("#" + _StaticConfig.TagIDCollection.Btn_Send).attr("disabled", false);
        $("#" + _StaticConfig.TagIDCollection.Btn_Send).val("获取短信激活码");
    }

    var _web_RootPath = "/";

    //获取已发送手机验证码次数
    function _sendMobileCount() {
        if (_flag) {
            var params = { "mobile": $("#txt_MobileNum").val() };
            $.ajax({
                type: "post",
                url: _web_RootPath + "Account/GetMobileInCache",
                data: $.param(params),
                async: false,
                success: function (TeamMsg) {
                    if (TeamMsg >= 4) {
                        _showMsg("err_send", "短信激活码一小时只能获取3次", "box001");
                        _time = TeamMsg;
                        _Timing();
                        _flag = _flag && false;
                    }
                    else {
                        _time = 0;
                        _flag = _flag && true;
                    }
                }
            })
        }
    }

    //判断手机是否绑定  IsAllowBinder
    function _photoBind() {
        if (_flag) {
            var params = { "mobileOrEmail": $("#txt_MobileNum").val() };
            $.ajax({
                type: "post",
                url: _web_RootPath + "Account/AllowBinder",
                async: false,
                data: $.param(params),
                success: function (data) {
                    if (data == "0") {
                        _showMsg("err_send", "手机号码已绑定", "box003");
                        _flag = _flag && false;
                    }
                    else {
                        _showMsg("err_send", "手机号码可以使用", "box002");
                        _flag = _flag && true;
                    }
                }
            });
        }
    }
    var _mobileReg = "^(13|15|18)[0-9]{9}$";
    //验证手机格式
    function _vailPhoto() {
        var mobiles = $("#AccountMobile").val();
        var expNum = new RegExp(RegexEnum.integer_Z);
        //alert(num);
        var exp = new RegExp(_mobileReg);
        if (mobiles == "") {
            _showMsg("err_send", "请输入手机号码", "box001");
            _flag = _flag && false;
            return;
        }
        else if (mobiles.length < 11) {
            if (expNum.test(mobiles)) {
                _showMsg("err_send", "请填写正确的手机号码", "box001");
            }
            else {
                _showMsg("err_send", "手机号码只能为数字", "box003");
            }
        }
        else if (!exp.test(mobiles)) {
            _showMsg("err_send", "手机号码格式错误", "box001");
            _flag = _flag && false;
            return;
        }
        else {
            _showMsg("err_send", " ", "box004");
            _flag = _flag && true;
        }
    }


    //发送手机验证码
    function _sendVerifyCode() {
        _flag = true;
        _vailPhoto();
        //_sendMobileCount();
        //_photoBind();
        if (_flag) {

            //发送验证码
            var params = { "mobile": $("#AccountMobile").val(), "vailKey": $("#vailSessionKey").val() };
            $.ajax({
                type: "post",
                url: _web_RootPath + "Account/SendSMS.aspx",
                data: $.param(params),
                success: function (data) {
                    $("#typeVail").click();
                    if (data == "1") {
                        _showMsg("err_send", "激活码已发送，请查收", "box002");
                        _showMsg("vail", " ", "box004");
                        //设置读秒
                        _time = _StaticConfig.FixTime;
                        //隐藏提示框
                        _hideNote(_StaticConfig.TagIDCollection.err_send);
                        //锁定发送
                        _lockSend();
                        //执行定时器
                        _Timing();
                    }
                    else if (data == "2") {

                        _showMsg("vail", "验证码有误", "box003");
                    }
                    else {
                        _showMsg("err_send", "发送有误，请重新发送", "box003");
                        _showMsg("vail", " ", "box004");
                    }
                }
            });


        }
    }

    function _checkVerify() {

        var mobileNum = $("#txt_MobileNum").val();
        var verifyNum = $("#VerifyCode").val();
        if (mobileNum == "") {
            _showMsg("err_VerifyCode", "手机号码为空", "box001");

        }
        else if (verifyNum == "") {
            _showMsg("err_VerifyCode", "请输入激活码", "box001");

        }
        else {
            _showMsg("err_VerifyCode", " ", "box004");
        }
    }

    //对外方法接口
    var _main = {
        //获取当前的手机号码
        Init: function () {
            var target = $("#" + _StaticConfig.TagIDCollection.Btn_Send);
            target.click(_sendVerifyCode);
        },
        checkVerify: function () {

            _checkVerify();
        },
        checkPhotot: function () {
            _flag = true;
            _vailPhoto();
            _photoBind();
            _checkVerify();
            return _flag;
        },
        focusFunction: function () {
            _flag = true;
            _showMsg("err_VerifyCode", " ", "box004");
            _showMsg("err_send", " ", "box004");
        },
        blurFunction: function () {
            _flag = true;
            _vailPhoto();
            _photoBind();
        },
        keyUpFunction: function () {
            _time = 0;
            //_showMsg("err_send", " ", "box004");
            _vailPhoto();
            var mob = $("#AccountMobile").val();
            if (mob.length >= 11) {
                _flag = true;
                _vailPhoto();

                if (_flag) {

                    $("div[id*='str']").remove();
                    var str = "<div id=\"str1\" class=\"tn-form-row1\">";
                    str += "<label class=\"tn-form-label\">";
                    str += "短信激活码：</label><span><input placeholder=\"未收到激活码,被杀毒软件拦截?\" maxlength=\"6\" onblur=\"ModifyMobileBinder.checkVerify()\" id=\"VerifyCode\" name=\"VerifyCode\" type=\"text\" class=\"tn-textbox tn-border-gray  valid form-control\" /></span><em>";
                    str += "&nbsp <input id=\"Btn_PopSend\"  type=\"button\" value=\"获取短信激活码\" class=\"\" style=\" height:38px; line-height: 40px \" />";
                    str += "    <span id=\"err_VerifyCode\">";
                    str += "      </span>";
                    str += "     </em>";
                    str += " </div>";

                    $("#mobilTextDiv").after(str);
                    $("#typeVail").click();
                    ModifyMobileBinder.Init();
                }
            } else {
                $("div[id*='str']").remove();
            }
        }

    };
    window.ModifyMobileBinder = _main;

    ModifyMobileBinder.Init();
})();


